import 'package:flutter/material.dart';
import 'package:kasax_demos2/day16_bloc_provider/page_provider_list.dart';
import 'package:provider/provider.dart';

/// Drawer管理
/// https://github.com/devsawaeologic/Flutter-ProviderExample
class ProviderForDrawerDemoHome extends StatefulWidget {
  const ProviderForDrawerDemoHome({Key? key}) : super(key: key);

  @override
  _ProviderForDrawerDemoHomeState createState() =>
      _ProviderForDrawerDemoHomeState();
}

class _ProviderForDrawerDemoHomeState extends State<ProviderForDrawerDemoHome> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ChangeNotifierProvider(
        create: (_) => DrawerProvider(),
        child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("Drawer管理"),
          ),
          body: Consumer<DrawerProvider>(
            builder: (context, provider, child) {
              return provider.currentPage;
            },
          ),
          drawer: DrawerListWidget(),
        ),
      ),
    );
  }
}

class DrawerListWidget extends StatelessWidget {
  const DrawerListWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var provider = Provider.of<DrawerProvider>(context);
    var list = ['home', 'profile', 'settings'];
    return Drawer(
      child: Column(
        children: [
          UserAccountsDrawerHeader(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: NetworkImage(
                        "https://img2.baidu.com/it/u=359180179,383614417&fm=26&fmt=auto"),
                    fit: BoxFit.cover)),
            accountName: Text("testDemo"),
            accountEmail: Text("testDemo@qq.com"),
            currentAccountPicture: CircleAvatar(
              backgroundImage: NetworkImage(
                  "https://img2.baidu.com/it/u=494972947,882635746&fm=26&fmt=auto"),
            ),
          ),
          ListView.separated(
              shrinkWrap: true,
              padding: EdgeInsets.zero,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(list[index]),
                  onTap: () {
                    provider.updatePage(index);
                    Navigator.pop(context);
                  },
                );
              },
              separatorBuilder: (context, index) {
                return Divider();
              },
              itemCount: list.length)
        ],
      ),
    );
  }
}

/// DrawerProvider 是ChangeNotifier的扩展类，用于调用notifyListener()方法后进行广播
class DrawerProvider extends ChangeNotifier {
  var _currentTab = [
    PageProviderUI("home"),
    PageProviderUI("profile"),
    PageProviderUI("settings")
  ];

  int _currentIndex = 0;
  late Widget currentPage;

  DrawerProvider() {
    currentPage = _currentTab[_currentIndex];
  }

  updatePage(int index) {
    _currentIndex = index;
    currentPage = _currentTab[_currentIndex];
    notifyListeners();
  }
}
